/**
 * Created by kangdaye on 16/6/5.
 */

//Main
app.controller('adConfigCtr',["$scope","$rootScope",'message',"Upload","adConfigCache","adConfigService", function($scope,$rootScope,message,Upload,adConfigCache,adConfigService) {
    $scope.configData = {
        banner : [],
        middle : [],
        sales : []
    };
    $scope.configMaxLength = {
        bannerMax : 6,
        salesMax : 6
    };

    $scope.tab = adConfigCache.listTab;
    $scope.tabSelectId = $scope.tab[0].id;
    $scope.selectHrefType = adConfigCache.selectHrefType;

    adConfigService.getConfig(function(data){
        $scope.configData = data.data;
    });

    $scope.bannerSubmit = function(){
        adConfigService.updateBanner({
            data : JSON.stringify($scope.configData.banner)
        },function(){
            message({text : '修改轮播图成功'});
        });
    };

    $scope.middleSubmit = function(){
        adConfigService.updateMiddle({
            data : JSON.stringify($scope.configData.middle)
        },function(){
            message({text : '修改推荐广告图成功'});
        });
    };

    $scope.salesSubmit = function(){
        adConfigService.updateSales({
            data : JSON.stringify($scope.configData.sales)
        },function(){
            message({text : '修改特价促销成功'});
        });
    };

    $scope.tabClick = function(id){
        $scope.tabSelectId = id;
    };

    $scope.upload = function (file,model,index) {
        Upload.upload({
            url: servicePath + 'ad/upload_image',
            data: {file: file}
        }).then(function (resp) {
            $scope.configData[model][index].image = resp.data.data;
        });
    };

    $scope.add = function(model){
        $scope.configData[model].push({});
    };

    $scope.delete = function(model,index){
        $scope.configData[model].splice(index,1);
    };

}]);
